﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Collections.TreeCollections;
using FeatureModelCore.Optimized;
using FeatureModelCore.FMCrossTreeConstraint;
using Collections;
using FeatureModelCore.FMAnalyzer;
using FeatureModelCore.FMInterface;

namespace FeatureModelCore.Optimized
{
    public class OptimizedRootFeature : TreeRoot, IOptimizableFeature
    {
        #region Properties
        public IFeatureModelObject StandardFeatureObject { get { return _standardTreeRoot; } }
        public FeatureTreeObjectExtension ObjectExtension { get { return _extension; } }
        public string FeatureName
        {
            get
            {
                return _standardTreeRoot.FeatureName;
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public string Annotation
        {
            get
            {
                return this._standardTreeRoot.Annotation;
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public FMProductCollection TmpResult
        {
            get { throw new NotImplementedException(); }
        }
        public FeatureNodeProperty[] Properties
        {
            get { throw new NotImplementedException(); }
        }
        public FMShapes.FMRectangle Shape
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public IFeature[] ChildrenNodes { get { return getChildrenFeature(); } }
        public IRelation[] ChildrenGroupRelationshipes { get { return getChildrenRelation(); } }
        public OptimizedPropertyForFeature[] PropertiesForFeature
        {
            get { return _propertiesForFeature.ToArray<OptimizedPropertyForFeature>(); }
        }
        public OptimizedPropertyForRelationship[] PropertiesForRelation
        {
            get { return _propertiesForRelation.ToArray<OptimizedPropertyForRelationship>(); }
        }

        #endregion

        #region Fields
        private FeatureTreeObjectExtension _extension;
        private FeatureTreeRoot _standardTreeRoot;
        private FMSet<OptimizedPropertyForFeature> _propertiesForFeature;
        private FMSet<OptimizedPropertyForRelationship> _propertiesForRelation;
        #endregion

        public OptimizedRootFeature(FeatureTreeRoot treeRoot)
            : base(treeRoot.ID)
        {
            _standardTreeRoot = treeRoot;
            _extension = new FeatureTreeObjectExtension(this);
            _propertiesForFeature = new FMSet<OptimizedPropertyForFeature>();
            _propertiesForRelation = new FMSet<OptimizedPropertyForRelationship>();
        }

        public override object Clone()
        {
            throw new NotImplementedException();
        }

        #region For Feature & Relation Properties
        public void AddPropertyForFeature(OptimizedPropertyForFeature property)
        {
            this._propertiesForFeature.Add(property);
        }

        public void AddPropertyForRelation(OptimizedPropertyForRelationship property)
        {
            this._propertiesForRelation.Add(property);
        }

        public OptimizedPropertyForFeature GetPropertyForFeature(TreeNodeIdentify id)
        {
            foreach (OptimizedPropertyForFeature f in _propertiesForFeature)
            {
                if (f.StandObjectID.Equals(id))
                {
                    return f;
                }
            }
            return null;    
        }

        public OptimizedPropertyForRelationship GetPropertyForRelation(TreeNodeIdentify id)
        {
            foreach (OptimizedPropertyForRelationship f in _propertiesForRelation)
            {
                if (f.StandObjectID.Equals(id))
                {
                    return f;
                }
            }
            return null;    
        }
        #endregion

        #region Get Objects
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private IRelation[] getChildrenRelation()
        {
            List<IRelation> relations = new List<IRelation>();
            foreach (INonRoot nonRoot in this.Children)
            {
                if (nonRoot is IRelation)
                {
                    relations.Add((IRelation)nonRoot);
                }
            }
            return relations.ToArray<IRelation>();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private IFeature[] getChildrenFeature()
        {
            List<IFeature> features = new List<IFeature>();
            foreach (IRelation relation in this.ChildrenGroupRelationshipes)
            {
                features.AddRange(relation.ChildrenNodes);
            }
            return features.ToArray<IFeature>();
        }
        #endregion
    }
}
